package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.CasesBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.CasesMapper;
import com.java2207.group1.pojo.Cases;
import com.java2207.group1.pojo.DrugStorage;
import com.java2207.group1.service.CasesService;
import com.java2207.group1.service.DrugStorageService;
import com.java2207.group1.vo.casesult.CasesBarVo;
import com.java2207.group1.vo.casesult.CasesPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Lenovo 2022/7/26 12:03
 */
@Service("casesService")
public class CasesServiceImpl implements CasesService {

    @Autowired
    CasesMapper casesMapper;

    @Autowired
    DrugStorageService drugStorageService;

    @Override
    public LayuiPageVo<Cases> caseLayuiPageData(Map<String, Object> params) {
        //把page和limit转从Object换成int类型
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");
        // mybatis 的 分页插件
        PageHelper.startPage(page, limit);

        List<Cases> cases = casesMapper.selectAllCaseByParams(params);

        // mybatis 的 分页数据结构
        PageInfo<Cases> casePageInfo = new PageInfo<>(cases);

        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中
        LayuiPageVo<Cases> caseLayuiPageVo = new LayuiPageVo<>();
        caseLayuiPageVo.setCode(0);                         // layui 默认 0 是正确的状态码
        caseLayuiPageVo.setMsg("分页列表数据");               // 提示信息
        caseLayuiPageVo.setCount(casePageInfo.getTotal());       // 总记录数
        caseLayuiPageVo.setData(casePageInfo.getList());        // 分页的列表数据

        return caseLayuiPageVo;
    }

    @Override
    public int addCase(Cases cas) {
        DrugStorage drug = new DrugStorage(cas.getDrugname(),cas.getDrugnumb());

        drugStorageService.reduceDrug(drug);

        return casesMapper.addCase(cas);
    }

    @Override
    public int deleteCase(Integer id) {
        return casesMapper.delectCase(id);
    }

    @Override
    public int deleteCaseByIds(String ids) {
        String[] arr = ids.split(",");
        return casesMapper.deleteCaseByIds(arr);
    }

    @Override
    public List<CasesBarVo> caseBarData() {
        List<CasesBarVo> list = casesMapper.caseBarData();
        return list;
    }

    @Override
    public List<CasesPieVo> casePieData() {
        List<CasesPieVo> list = casesMapper.casePieData();
        return list;
    }


    @Override
    public int updateCase(Cases cas) {

        DrugStorage drug = new DrugStorage(cas.getDrugname(),cas.getDrugnumb());

        drugStorageService.reduceDrug(drug);

        return casesMapper.updateCase(cas);
    }


    @Override
    public List<CasesBo> casExcelData(Map<String, Object> params) {

        List<Cases> caseList = casesMapper.selectAllCaseByParams(params);

        //把 po 转为 bo
        List<CasesBo> casesBos = caseList.stream().map((cas) -> {
            CasesBo casesBo = new CasesBo();
            casesBo.setId(cas.getId());
            casesBo.setName(cas.getName());
            casesBo.setSex(cas.getSex());
            casesBo.setAge(cas.getAge());
            casesBo.setSymptom(cas.getSymptom());
            casesBo.setPrescription(cas.getPrescription());
            casesBo.setDate(cas.getDate());

            return casesBo;
        }).collect(Collectors.toList());

        return casesBos;
    }


}
